let utils = data["utils"];
let STUtil = data["STUtil"];
let Node = data["Node"];
let SplayTree = data["SplayTree"];
let isInBbox = data["isInBbox"];
let getBboxOverlap = data["getBboxOverlap"];
let cmp = data["cmp"];
let crossProduct = data["crossProduct"];
let dotProduct = data["dotProduct"];
let compareVectorAngles = data["compareVectorAngles"];
let sineOfAngle = data["sineOfAngle"];
let cosineOfAngle = data["cosineOfAngle"];
let closestPoint = data["closestPoint"];
let horizontalIntersection = data["horizontalIntersection"];
let verticalIntersection = data["verticalIntersection"];
let intersection = data["intersection"];
let perpendicular = data["perpendicular"];
console.log("08PolygonClipping_rounder");

function CoordRounder() {

  this.round = function (coord) {
    const node = this.tree.add(coord)

    const prevNode = this.tree.prev(node)
    if (prevNode !== null && cmp(node.key, prevNode.key) === 0) {
      this.tree.remove(coord)
      return prevNode.key
    }

    const nextNode = this.tree.next(node)
    if (nextNode !== null && cmp(node.key, nextNode.key) === 0) {
      this.tree.remove(coord)
      return nextNode.key
    }

    return coord
  }

  this.tree = new SplayTree()
  // preseed with 0 so we don't end up with values < Number.EPSILON
  this.round(0);
}

function PtRounder() {
  this.reset = function () {
    this.xRounder = new CoordRounder()
    this.yRounder = new CoordRounder()
  }

  this.round = function (x, y) {
    return {
      x: this.xRounder.round(x),
      y: this.yRounder.round(y),
    }
  }
  this.reset();
}






// singleton available by import
const rounder = new PtRounder()

return {
  "utils": utils,
  "STUtil": STUtil,
  "Node": Node,
  "SplayTree": SplayTree,
  "isInBbox": isInBbox,
  "getBboxOverlap": getBboxOverlap,
  "cmp": cmp,
  "crossProduct": crossProduct,
  "dotProduct": dotProduct,
  "compareVectorAngles": compareVectorAngles,
  // "length":length,
  "sineOfAngle": sineOfAngle,
  "cosineOfAngle": cosineOfAngle,
  "closestPoint": closestPoint,
  "horizontalIntersection": horizontalIntersection,
  "verticalIntersection": verticalIntersection,
  "intersection": intersection,
  "perpendicular": perpendicular,
  "rounder": rounder
}